//
//  RHCommonDataCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/2/27.
//

#import "RHCommonDataCell.h"

 NSString *const CommonNormalCell    = @"CommonNormalCell";//普通cell,左右两边文字
 NSString *const CommonStatusCell    = @"CommonStatusCell";//状态cell
 NSString *const CommonIndicatorCell = @"CommonIndicatorCell";//
 NSString *const CommonProgressCell  = @"CommonProgressCell";

@interface RHCommonDataCell()

@property (nonatomic, strong) UIImageView *indicatorImg;

@property (nonatomic, strong) UIProgressView *progressView;

@property (nonatomic, strong) UIView *pointView;
@end
@implementation RHCommonDataCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self setupUI];
    }
    return self;
}

-(void)setupUI
{
    kWeakSelf;
    UIView *bgView = [[UIView alloc]init];
    bgView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:bgView];
    self.contentView.backgroundColor = CFTabViewBgColor;
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 15, 0, 15));
    }];
    
    _titleLab = [[QMUILabel alloc]init];
    _titleLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:0.6];
    _titleLab.textAlignment = NSTextAlignmentLeft;
    _titleLab.font = kSYSTEMFONT(14);
    [bgView addSubview:_titleLab];
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.centerY.mas_offset(0);
    }];
    
    _contentLab = [[QMUILabel alloc]init];
    _contentLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:0.8];
    _contentLab.textAlignment = NSTextAlignmentLeft;
    _contentLab.font = kSYSTEMFONT(14);
    [bgView addSubview:_contentLab];
    [_contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-15);
        make.centerY.mas_offset(0);
    }];
    
    if([self.reuseIdentifier isEqualToString:CommonStatusCell]){
        _pointView = [[UIView alloc]init];
        _pointView.backgroundColor = kCOLOR_HEX(@"55C42A");
        _pointView.layer.cornerRadius = 4;
        _pointView.layer.masksToBounds = YES;
        [bgView addSubview:_pointView];
        [_pointView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakSelf.contentLab.mas_left).offset(-10);
            make.centerY.mas_offset(0);
            make.size.equalTo(@(CGSizeMake(8, 8)));
        }];
    }
    
    if([self.reuseIdentifier isEqualToString:CommonProgressCell]){
        _progressView = [[UIProgressView alloc]init];
        _progressView.progress = 0.5;
        _progressView.trackTintColor = kCOLOR_HEX(@"E6E6E6");
        _progressView.progressTintColor = kCOLOR_HEX(@"678EF2");
        [bgView addSubview:_progressView];
        [_progressView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.equalTo(@(CGSizeMake(45, 5)));
            make.right.equalTo(weakSelf.contentLab.mas_left).offset(-5);
            make.centerY.mas_offset(0);
        }];
    }
    
    if([self.reuseIdentifier isEqualToString:CommonIndicatorCell]){
        _indicatorImg = [[UIImageView alloc]init];;
        _indicatorImg.image = [UIImage imageNamed:@"block_power_up"];
        [bgView addSubview:_indicatorImg];
        [_indicatorImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.equalTo(@(CGSizeMake(15, 15)));
            make.right.equalTo(weakSelf.contentLab.mas_left).offset(-5);
            make.centerY.mas_offset(0);
        }];
    }
}


-(void)setDataModel:(RHDevDataModel *)dataModel
{
    _dataModel = dataModel;
    self.titleLab.text = [NSString stringWithFormat:@"%@:",dataModel.alias.rh_localized];
    self.contentLab.text = [NSString stringWithFormat:@"%@%@",dataModel.content,ValidNullData(dataModel.unit)];
    if ([dataModel.keyCode isEqualToString:@"Pck_TV"]&&dataModel.content.length>0) {
        NSNumber *tv = [NSNumber numberWithFloat:[dataModel.content floatValue]];
        self.contentLab.text = [NSString stringWithFormat:@"%@%@",[tv decimalPlacesWithRoundUpWithDigit:1],ValidNullData(dataModel.unit)];;
    }
    if(dataModel.content.length == 0){
        self.contentLab.text = [NSString stringWithFormat:@"--%@",ValidNullData(dataModel.unit)];
    }
    
    NSInteger cellType = dataModel.type;
    if(cellType == CommonCellTypeProgress){
        self.progressView.progress = [dataModel.content integerValue]/100.0;
        
    }
    if(cellType == CommonCellTypeNormal){
        //枚举类型
        if(dataModel.enums.count>0){
            //有值
            if(dataModel.content.length>0){
                __block NSInteger index = -1;
                [dataModel.enums enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    if([dataModel.content integerValue] == [obj integerValue]){
                        index = idx;
                    }
                }];
                if(index >= 0 &&index<dataModel.enumNames.count){
                    self.contentLab.text = dataModel.enumNames[index];
                }else{
                    self.contentLab.text = [NSString stringWithFormat:@"--%@",ValidNullData(dataModel.unit)];
                }
            }else{
                //无值
                self.contentLab.text = [NSString stringWithFormat:@"--%@",ValidNullData(dataModel.unit)];
            }
        }
    }
    if(cellType == CommonCellTypeStatus){
        
        if(dataModel.enums.count>0&&dataModel.content.length>0){
            __block NSInteger index = -1;
            [dataModel.enums enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if([dataModel.content integerValue] == [obj integerValue]){
                    index = idx;
                }
            }];
            if(index >= 0 &&index<dataModel.enumNames.count){
                self.contentLab.text = dataModel.enumNames[index];
                //正常
                
                //异常
            }else{
                self.contentLab.text = @"--";
            }
        }else{
            self.contentLab.text = @"--";
        }
        
    }
}

@end
